<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {

	public $users = [];

	public function __construct(){
		parent::__construct();
		$this->users = [
			[
				"id" => 1,
				"username" => "admin",
				"password" => "06a76abeee71b5138ca734384141460d",//wefish409
			]
		];
	}

	public function index(){
		$this->load->view('login/index');
	}

	public function asyncLogin(){

		$username = isset($_POST["username"]) ? addslashes($_POST["username"]) : "";
		$password = isset($_POST["password"]) ? addslashes($_POST["password"]) : "";


		$user = \models\User::getUserByUsername($username);

		if(empty($user)){
			exit(json_encode(["code"=>-1, "info"=>"用户不存在"], JSON_UNESCAPED_UNICODE));
		}

		if(md5($password) != $user["password"]){
			exit(json_encode(["code"=>-1, "info"=>"密码错误!"], JSON_UNESCAPED_UNICODE));
		}


		session_start();
		$_SESSION["loginUid"] = $user["id"];

		exit(json_encode(["code"=>1, "info"=>"login success!"], JSON_UNESCAPED_UNICODE));


	}

	//logout
	public function logout(){
		session_start();
		$_SESSION["loginUid"] = null;
		header("Location:"."/");
	}



}
